package com.uenpay.im.ui.old.activity.crowdfund;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.qiyunxin.android.http.HttpInvokeResultCode;
import com.uenpay.im.R;
import com.uenpay.im.callback.IOnBottomDialogListener;
import com.uenpay.im.utilities.EditTextUtils;
import com.uenpay.im.utilities.ToastUtil;
import com.uenpay.im.weight.DialogUtility;
import com.uenpay.util.app.base.BaseActivity;

/**
 * @author SL
 * @version 1.0
 * @copyRight http://www.qiyunxin.com
 * @E-mail:
 * @date 创建时间：2016-5-7 下午1:07:51
 * @function 江湖救急
 */
public class CrowdFundActivity extends BaseActivity {
    private View loading;
    private EditText amount_et, memo_et;
    private Button comfirm_btn;
    private String chatid;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        isFullScreen = true;
        super.onCreate(savedInstanceState);
    }

    @Override
    public View bindView() {
        return null;
    }

    @Override
    public int bindLayout() {
        return R.layout.activity_crowd_fund_layout;
    }

    @Override
    public void initView(View view) {
        chatid = getIntent().getStringExtra("chatid");
        initView();
        initListener();
    }

    protected void initView() {
        ((TextView) findViewById(R.id.title_textview))
                .setText(R.string.crowdfund);
        memo_et = (EditText) findViewById(R.id.memo_et);
        amount_et = (EditText) findViewById(R.id.amount_et);
        comfirm_btn = (Button) findViewById(R.id.comfirm_btn);
        loading = findViewById(R.id.loading);
    }

    protected void initListener() {
        amount_et.addTextChangedListener(new TextWatcher() {

            @Override
            public void onTextChanged(CharSequence arg0, int arg1, int arg2,
                                      int arg3) {
                checkBtnStatus();
                EditTextUtils.inputDecimal(amount_et, arg0.toString());
            }

            @Override
            public void beforeTextChanged(CharSequence arg0, int arg1,
                                          int arg2, int arg3) {
            }

            @Override
            public void afterTextChanged(Editable arg0) {
            }
        });

        comfirm_btn.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                String amount = amount_et.getText().toString();
                String memo = memo_et.getText().toString();
                if (!TextUtils.isEmpty(amount)) {
                    double temp = Double.valueOf(amount);
                    if (temp <=0.0) {
                        ToastUtil.showToast(getApplicationContext(), "金额不能小于0");
                        return;
                    }
                    if (temp > 10000) {
                        showMoreDialog();
                    } else {
                        createTransfer(amount, memo);
                    }
                }
            }
        });
    }

    private void showMoreDialog() {
        DialogUtility.showSingleBtnDialog(CrowdFundActivity.this,
                R.string.crowdfund_max, R.string.comfirm, true,
                new IOnBottomDialogListener() {

                    @Override
                    public void onClicked() {

                    }
                });
    }

    private void checkBtnStatus() {
        String money = amount_et.getText().toString();
        if (!TextUtils.isEmpty(money)) {
            comfirm_btn.setEnabled(true);
            comfirm_btn.setBackgroundResource(R.drawable.button_bg_normal);
        } else {
            comfirm_btn.setBackgroundResource(R.drawable.button_bg_gray);
            comfirm_btn.setEnabled(false);
        }
    }

    /**
     * 创建众筹
     *
     * @param amount
     * @param memo
     */
    private void createTransfer(String amount, final String memo) {
        loading.setVisibility(View.VISIBLE);
        CrowdFundHandle.createCrowdFund(chatid, amount, memo,
                new CrowdFundHandle.ICreateCrowdFundLisenter() {

                    @Override
                    public void onCreateCFResult(int status, String msg,
                                                 String crowdFundId, String amount) {
                        loading.setVisibility(View.GONE);
                        if (status == HttpInvokeResultCode.RESULT_OK) {
                            Intent intent = new Intent();
                            intent.putExtra("amount", amount);
                            intent.putExtra("memo", memo);
                            intent.putExtra("fundid", crowdFundId);
                            setResult(RESULT_OK, intent);
                            finish();
                        }
                    }
                });
    }

    @Override
    public void onBackPressed() {
        finish();
        super.onBackPressed();
    }
}
